package com.csjbot.snowbot_rogue.utils;


import com.csjbot.csjbase.log.Csjlogger;
import com.csjbot.snowbot_rogue.platform.SnowBotManager;
import com.csjbot.snowbot_rogue.platform.SnowBotNetwokListen;
import com.iflytek.aiui.uartkit.UARTAgent;
import com.iflytek.aiui.uartkit.entity.WIFIConfPacket;
import com.iflytek.aiui.uartkit.util.PacketBuilder;

import java.io.Serializable;

/**
 * Created by Administrator on 2016/10/31 0031.
 */

public class CSJWifiUtils {
    public static class CSJScanResult implements Serializable {
        public String bssid;
        public String ssid;
        public String secret;
        public int signal;
        public String passwd = "";

        @Override
        public String toString() {
            return "CSJScanResult  bssid : " + bssid + "; ssid : " + ssid + "; secret " + secret + "; signal " + signal;
        }
    }

    public static void connectWifi(String ssid, String passwd, SnowBotNetwokListen listener) {
        SnowBotManager.getInstance().configNetWork(ssid, passwd, listener);

        try {
            UARTAgent agent = UARTAgent.getUARTAgent();
            agent.sendMessage(PacketBuilder.obtainWIFIConfPacket(WIFIConfPacket.WIFIStatus.CONNECTED,
                    WIFIConfPacket.EncryptMethod.WPA, ssid, passwd));
        } catch (NullPointerException e) {
            Csjlogger.debug("UARTAgent is Null");
        }

        Csjlogger.debug("Try  to connect  wifi " + ssid + " " + passwd);
    }

    public static void connectWifi(CSJScanResult result, SnowBotNetwokListen listener) {
        SnowBotManager.getInstance().configNetWork(result.ssid, result.passwd, listener);

        try {
            UARTAgent agent = UARTAgent.getUARTAgent();
            agent.sendMessage(PacketBuilder.obtainWIFIConfPacket(WIFIConfPacket.WIFIStatus.CONNECTED,
                    result.secret.contains("WPA") ? WIFIConfPacket.EncryptMethod.WPA : WIFIConfPacket.EncryptMethod.OPEN, result.ssid, result.passwd));
        } catch (NullPointerException e) {
            Csjlogger.debug("UARTAgent is Null");
        }

        Csjlogger.debug("Try  to connect  wifi " + result.ssid + " " + result.passwd);
    }

    public static boolean connectWifiNew(CSJScanResult result, SnowBotNetwokListen listener) {
//        SnowBotManager.getInstance().configNetWorkNew(listener);
        if (listener != null) {
            listener.configWifiState(true);
        }

        try {
            UARTAgent agent = UARTAgent.getUARTAgent();
            agent.sendMessage(PacketBuilder.obtainWIFIConfPacket(WIFIConfPacket.WIFIStatus.CONNECTED,
                    result.secret.contains("WPA") ? WIFIConfPacket.EncryptMethod.WPA : WIFIConfPacket.EncryptMethod.OPEN, result.ssid, result.passwd));
        } catch (NullPointerException e) {
            Csjlogger.debug("UARTAgent is Null");
        }

        Csjlogger.debug("Try  to connect  wifi " + result.ssid + " " + result.passwd);
        return true;
    }
}
